package com.ruoyi.web.web.controller;

import com.github.pagehelper.PageInfo;
import com.ruoyi.system.service.ITTransactionDetailService;
import com.ruoyi.web.controller.system.SysLoginController;
import com.ruoyi.web.core.config.AppConf;
import com.ruoyi.accountant.domain.TransactionDetail;
import com.ruoyi.accountant.service.TransactionDetailService;
import com.ruoyi.web.web.form.TransactionDetailListForm;
import com.ruoyi.web.web.form.Validation;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/***
 * 交易记录信息
 */
@Controller
@RequestMapping("/transactionDetail")
public class TransactionDetailController {

    @Autowired
    private TransactionDetailService transactionDetailService;

    @Resource
    private ITTransactionDetailService itTransactionDetailService;

    @Resource
    private ITTransactionDetailService itransactionDetailService;

    @Resource
    private SysLoginController sysLoginController;

    /***
     * 完成交易
     */
    @GetMapping("/finish")
    public String finish(Long tid) {
        transactionDetailService.finish(tid);
        return "redirect:/pages/detail/transaction.html";
    }

    /***
     * 删除交易
     * @return
     */
    @GetMapping("/delete")
    public String delete(Long tid) {
        transactionDetailService.deleteById(tid);

        return "redirect:/pages/detail/transaction.html";
    }

    /***
     * 恢復交易
     * @return
     */
    @GetMapping("/recover")
    public String recover(Long tid) {
        transactionDetailService.recover(tid);
        return "redirect:/pages/detail/deleted.html";
    }

    /***
     * 更改信息
     * @param transactionDetail
     * @return
     */
    @PostMapping("/update")
    public String update(TransactionDetail transactionDetail) {
        transactionDetailService.updateByForm(transactionDetail);
        return "redirect:/pages/detail/transaction.html";
    }

    @PostMapping("/export")
    public void export(TransactionDetailListForm form, HttpServletResponse response) throws IOException {
        Validation validate = form.validate();
        if(validate.getOk()) {
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment;fileName=transaction.xls");

            List<TransactionDetail> allByForm =transactionDetailService.findAllByForm(form);
            HSSFWorkbook workbook = new HSSFWorkbook();
            HSSFSheet sheet = workbook.createSheet();
            HSSFRow row = sheet.createRow(0);

            row.createCell(0, CellType.STRING).setCellValue("交易类型");
            row.createCell(1, CellType.STRING).setCellValue("收付日期");
            row.createCell(2, CellType.STRING).setCellValue("憑證日期");
            row.createCell(3, CellType.STRING).setCellValue("收支原因");
            row.createCell(4, CellType.STRING).setCellValue("交易金額");
            row.createCell(5, CellType.STRING).setCellValue("币种类型");
            row.createCell(6, CellType.STRING).setCellValue("資金賬戶");
            row.createCell(7, CellType.STRING).setCellValue("附註說明");
            row.createCell(8, CellType.STRING).setCellValue("對象");
            row.createCell(9, CellType.STRING).setCellValue("標籤");
            row.createCell(10, CellType.STRING).setCellValue("營業稅");
            row.createCell(11, CellType.STRING).setCellValue("發票號碼");


            int rowNum = 1;
            for (TransactionDetail detail : allByForm) {
                System.out.println(detail);
                HSSFRow row1 = sheet.createRow(rowNum++);

                row1.createCell(0, CellType.STRING).setCellValue(StringUtils.isEmpty(detail.getDtype()) ? "" : detail.getDtype());
                row1.createCell(1, CellType.STRING).setCellValue(StringUtils.isEmpty(detail.getTransactionDate()) ? "" : AppConf.DATE_FORMAT.format(detail.getTransactionDate()));
                row1.createCell(2, CellType.STRING).setCellValue(StringUtils.isEmpty(detail.getProofDate()) ? "": AppConf.DATE_FORMAT.format(detail.getProofDate()));
                row1.createCell(3, CellType.STRING).setCellValue(detail.getOfReason() != null ? detail.getOfReasonObject().getRname() : "");
                row1.createCell(4, CellType.STRING).setCellValue(detail.getMoney().toString());
                row1.createCell(5, CellType.STRING).setCellValue(detail.getOfCurrency() != null ? detail.getOfCurrencyObject().getCnName() : "");
                row1.createCell(6, CellType.STRING).setCellValue(detail.getOfAccountObject().getAname());
                row1.createCell(7, CellType.STRING).setCellValue(detail.getComment());
                row1.createCell(8, CellType.STRING).setCellValue(detail.getObject());
                row1.createCell(9, CellType.STRING).setCellValue(detail.getTag());
                row1.createCell(10, CellType.STRING).setCellValue(detail.getTax() != null ? detail.getTax().toString() : "");
                row1.createCell(11, CellType.STRING).setCellValue(detail.getInvoice());
            }

            workbook.write(response.getOutputStream());
            workbook.close();
        }
    }

}
